package java_jdbc;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

import util.DBUtil;



/*
 * 3:在控制台输入一个学生的全部信息，保存到数据库，
注意日期的处理(PreparedStatement)
 */
public class jbdc4 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Connection con = DBUtil.getConnection();
		PreparedStatement pst = null;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Scanner in = new Scanner(System.in);
		System.out.println("请输入姓名：");
		String sname = in.next();
		System.out.println("请输入性别：");
		String sex = in.next();
		System.out.println("请输入电话号码");
		String tel = in.next();
		System.out.println("请输入qq号码");
		String qq = in.next();
		System.out.println("请输入wx号码");
		String wx = in.next();
		System.out.println("请输入年级号码");

		long gid = in.nextLong();
		System.out.println("请输入入学日期：");
		Date enterdate = Date.valueOf(in.next());
		System.out.println("请输入来自");
		String fromcity = in.next();
		System.out.println("请输入生日：");
		Date birthday = Date.valueOf(in.next());
		System.out.println("请输入老家");
		String address = in.next();
		System.out.println("请输入密码");
		String pass = in.next();
		try {
			String sql = "insert into s_student (sname,sex,telephone,qq,wechat,gradeid,enterdate,fromcity,birthday,address,pass) "
					+ "VALUES (?,?,?,?,?,?,?,?,?,?,?)";

			pst = con.prepareStatement(sql);

			pst.setString(1, sname);
			pst.setString(2, sex);
			pst.setString(3, tel);
			pst.setString(4, qq);
			pst.setString(5, wx);
			pst.setLong(6, gid);
			pst.setDate(7, enterdate);
			pst.setString(8, fromcity);
			pst.setDate(9, birthday);
			pst.setString(10, address);
			pst.setString(11, pass);
			pst.executeUpdate();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			DBUtil.close(pst);
			DBUtil.close(con);
		}
	}

}